package com.jzo2o.foundations.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeItemService;
import com.jzo2o.foundations.service.IServeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-16
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {

    @Resource
    private IServeItemService serveItemService;

    @Resource
    private IRegionService regionService;

    /**
     * 根据区域ID,分区查询该区域下的列表
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO dto) {
        return PageHelperUtils.selectPage(dto,
                ()-> baseMapper.queryServeByRegioned(dto.getRegionId()));

    }

    /**
     * 给指定区域新增服务
     *
     * @param dtoList
     */
    @Override
    @Transactional
    public void addBatch(List<ServeUpsertReqDTO> dtoList) {
        if (CollUtils.isEmpty(dtoList)){
            throw new ForbiddenOperationException("新增服务不能为空");
        }
        //1. 循环遍历dtoList集合,添加区域服务
        for (ServeUpsertReqDTO dto : dtoList) {
            //判断服务项是否存在,且状态为启用
            ServeItem serveItem = serveItemService.getById(dto.getServeItemId());
            if (serveItem == null || serveItem.getActiveStatus()!= FoundationStatusEnum.ENABLE.getStatus()){
                throw new ForbiddenOperationException("服务项不存在或未启用");
            }

            //判断同一个区域的服务项是否重复添加
            Serve serve = this.lambdaQuery()
                    .eq(Serve::getRegionId, dto.getRegionId())
                    .eq(Serve::getServeItemId, dto.getServeItemId())
                    .one();
            if (serve != null){
                throw new ForbiddenOperationException("该区域下已存在该服务项");
            }

            //添加服务项
            serve = BeanUtil.copyProperties(dto, Serve.class);
            //补全city_code
            Region region = regionService.getById(dto.getRegionId());
            serve.setCityCode(region.getCityCode());

            boolean save = this.save(serve);
            if (!save){
                throw new ForbiddenOperationException("新增服务失败");
            }
        }

    }

    /**
     * 上架服务
     *
     * @param id 服务id
     * @return
     */
    @Override
    @Transactional
    public Serve onSale(Long id) {
        Serve serve = this.getById(id);
        if (serve == null || serve.getSaleStatus() == FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务不存在或已上架");
        }
        ServeItem serveItem = serveItemService.getById(serve.getServeItemId());
        if (serveItem == null || serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("服务项不存在或未启用");
        }

        boolean update = this.lambdaUpdate()
                .set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus())
                .eq(Serve::getId, id)
                .update();

        if (!update){
            throw new ForbiddenOperationException("上架服务失败");
        }

        return this.getById(id);

    }
}
